{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### ❇️ Sorting pandas 🐼 dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>species</th>\n",
       "      <th>island</th>\n",
       "      <th>bill_length_mm</th>\n",
       "      <th>bill_depth_mm</th>\n",
       "      <th>flipper_length_mm</th>\n",
       "      <th>body_mass_g</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>Chinstrap</td>\n",
       "      <td>Dream</td>\n",
       "      <td>50.5</td>\n",
       "      <td>19.6</td>\n",
       "      <td>201.0</td>\n",
       "      <td>4050.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>171</th>\n",
       "      <td>Chinstrap</td>\n",
       "      <td>Dream</td>\n",
       "      <td>49.2</td>\n",
       "      <td>18.2</td>\n",
       "      <td>195.0</td>\n",
       "      <td>4400.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Dream</td>\n",
       "      <td>38.3</td>\n",
       "      <td>19.2</td>\n",
       "      <td>189.0</td>\n",
       "      <td>3950.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>259</th>\n",
       "      <td>Gentoo</td>\n",
       "      <td>Biscoe</td>\n",
       "      <td>48.7</td>\n",
       "      <td>15.7</td>\n",
       "      <td>208.0</td>\n",
       "      <td>5350.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       species  island  bill_length_mm  bill_depth_mm  flipper_length_mm  \\\n",
       "167  Chinstrap   Dream            50.5           19.6              201.0   \n",
       "171  Chinstrap   Dream            49.2           18.2              195.0   \n",
       "88      Adelie   Dream            38.3           19.2              189.0   \n",
       "259     Gentoo  Biscoe            48.7           15.7              208.0   \n",
       "\n",
       "     body_mass_g   sex  \n",
       "167       4050.0  Male  \n",
       "171       4400.0  Male  \n",
       "88        3950.0  Male  \n",
       "259       5350.0  Male  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We pick a random sample of 4 rows\n",
    "df = pd.read_csv('penguins.csv').sample(frac=0.012)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>species</th>\n",
       "      <th>island</th>\n",
       "      <th>bill_length_mm</th>\n",
       "      <th>bill_depth_mm</th>\n",
       "      <th>flipper_length_mm</th>\n",
       "      <th>body_mass_g</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Dream</td>\n",
       "      <td>38.3</td>\n",
       "      <td>19.2</td>\n",
       "      <td>189.0</td>\n",
       "      <td>3950.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>Chinstrap</td>\n",
       "      <td>Dream</td>\n",
       "      <td>50.5</td>\n",
       "      <td>19.6</td>\n",
       "      <td>201.0</td>\n",
       "      <td>4050.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>171</th>\n",
       "      <td>Chinstrap</td>\n",
       "      <td>Dream</td>\n",
       "      <td>49.2</td>\n",
       "      <td>18.2</td>\n",
       "      <td>195.0</td>\n",
       "      <td>4400.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>259</th>\n",
       "      <td>Gentoo</td>\n",
       "      <td>Biscoe</td>\n",
       "      <td>48.7</td>\n",
       "      <td>15.7</td>\n",
       "      <td>208.0</td>\n",
       "      <td>5350.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       species  island  bill_length_mm  bill_depth_mm  flipper_length_mm  \\\n",
       "88      Adelie   Dream            38.3           19.2              189.0   \n",
       "167  Chinstrap   Dream            50.5           19.6              201.0   \n",
       "171  Chinstrap   Dream            49.2           18.2              195.0   \n",
       "259     Gentoo  Biscoe            48.7           15.7              208.0   \n",
       "\n",
       "     body_mass_g   sex  \n",
       "88        3950.0  Male  \n",
       "167       4050.0  Male  \n",
       "171       4400.0  Male  \n",
       "259       5350.0  Male  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sort on the basis of species; lexicographic sorting occurs\n",
    "df.sort_values(by=['species'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>species</th>\n",
       "      <th>island</th>\n",
       "      <th>bill_length_mm</th>\n",
       "      <th>bill_depth_mm</th>\n",
       "      <th>flipper_length_mm</th>\n",
       "      <th>body_mass_g</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Dream</td>\n",
       "      <td>38.3</td>\n",
       "      <td>19.2</td>\n",
       "      <td>189.0</td>\n",
       "      <td>3950.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>171</th>\n",
       "      <td>Chinstrap</td>\n",
       "      <td>Dream</td>\n",
       "      <td>49.2</td>\n",
       "      <td>18.2</td>\n",
       "      <td>195.0</td>\n",
       "      <td>4400.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>Chinstrap</td>\n",
       "      <td>Dream</td>\n",
       "      <td>50.5</td>\n",
       "      <td>19.6</td>\n",
       "      <td>201.0</td>\n",
       "      <td>4050.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>259</th>\n",
       "      <td>Gentoo</td>\n",
       "      <td>Biscoe</td>\n",
       "      <td>48.7</td>\n",
       "      <td>15.7</td>\n",
       "      <td>208.0</td>\n",
       "      <td>5350.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       species  island  bill_length_mm  bill_depth_mm  flipper_length_mm  \\\n",
       "88      Adelie   Dream            38.3           19.2              189.0   \n",
       "171  Chinstrap   Dream            49.2           18.2              195.0   \n",
       "167  Chinstrap   Dream            50.5           19.6              201.0   \n",
       "259     Gentoo  Biscoe            48.7           15.7              208.0   \n",
       "\n",
       "     body_mass_g   sex  \n",
       "88        3950.0  Male  \n",
       "171       4400.0  Male  \n",
       "167       4050.0  Male  \n",
       "259       5350.0  Male  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sort on the basis of species; break ties using bill_length when species are same\n",
    "df.sort_values(by=['species', 'bill_length_mm'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>species</th>\n",
       "      <th>island</th>\n",
       "      <th>bill_length_mm</th>\n",
       "      <th>bill_depth_mm</th>\n",
       "      <th>flipper_length_mm</th>\n",
       "      <th>body_mass_g</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Dream</td>\n",
       "      <td>38.3</td>\n",
       "      <td>19.2</td>\n",
       "      <td>189.0</td>\n",
       "      <td>3950.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>259</th>\n",
       "      <td>Gentoo</td>\n",
       "      <td>Biscoe</td>\n",
       "      <td>48.7</td>\n",
       "      <td>15.7</td>\n",
       "      <td>208.0</td>\n",
       "      <td>5350.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>171</th>\n",
       "      <td>Chinstrap</td>\n",
       "      <td>Dream</td>\n",
       "      <td>49.2</td>\n",
       "      <td>18.2</td>\n",
       "      <td>195.0</td>\n",
       "      <td>4400.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>Chinstrap</td>\n",
       "      <td>Dream</td>\n",
       "      <td>50.5</td>\n",
       "      <td>19.6</td>\n",
       "      <td>201.0</td>\n",
       "      <td>4050.0</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       species  island  bill_length_mm  bill_depth_mm  flipper_length_mm  \\\n",
       "88      Adelie   Dream            38.3           19.2              189.0   \n",
       "259     Gentoo  Biscoe            48.7           15.7              208.0   \n",
       "171  Chinstrap   Dream            49.2           18.2              195.0   \n",
       "167  Chinstrap   Dream            50.5           19.6              201.0   \n",
       "\n",
       "     body_mass_g   sex  \n",
       "88        3950.0  Male  \n",
       "259       5350.0  Male  \n",
       "171       4400.0  Male  \n",
       "167       4050.0  Male  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# provide a custom sorting function, Here 👇 sorting occurrs based on sum of 'bill_length_mm' & 'bill_depth_mm'\n",
    "df.sort_values(by=['bill_length_mm', 'bill_depth_mm'], key=lambda x: df['bill_length_mm'] + df['bill_depth_mm'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### ❇️ Hope you enjoyed reading!! 📖 \n",
    "##### ❇️ follow → @akshay_pachaar  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
